﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetfractionHub.Network.Protocol;
using NetfractionHub.Network.Protocol.BASE.Factory;

namespace NetfractionHub.LocalMachine.BotCommands.Handler
{
    [CommandHandler("resetpass")]
    public class ResetPassword : CommandHandler
    {
        public override BotReturnStatus HandleCommandMessage(NetfractionHub.RemoteMachine.Node source, CommandMessage message)
        {
            if (message.StringArguments.Count == 1)
                if (message.StringArguments.ContainsKey("user"))
                    return SendResetMessage(source, message.StringArguments["user"]);
            else if (message.UnnamedArguments.Count == 1)
                return SendResetMessage(source, message.UnnamedArguments[0]);
            else if (message.UnnamedArguments.Count == 0)
                return SendResetMessage(source, source.Identity.Nickname);
            else
                return new BotReturnStatus(false);

            return new BotReturnStatus(false);
        }

        private BotReturnStatus SendResetMessage(NetfractionHub.RemoteMachine.Node source, string username)
        {
            RemoteMachine.Node user = Network.ConnectionManager.GetConnectedNodeByNickname(username);
            if (user == null)
                return new BotReturnStatus(false, string.Format("Cannot reset password, the user '{0}' does not exist.", username));

            if (!Security.Permission.HasPermission(source.SecurityIdentity, typeof(Security.Permissions.ChangePasswordPermission)))
                return new BotReturnStatus(false, Security.Permission.GetDeniedString(typeof(Security.Permissions.ChangePasswordPermission)));

            if (user == source)
            {
                if (user.SecurityIdentity.IsRegistered)
                {
                    this.ParentBot.SendPrivateMessageFromBot(user, "Please enter a new password using the !pass command. You must remember this password to connect to the hub.\nUsage: !pass \"newpassword\"");
                    user.SecurityIdentity.NewPasswordRequestActive = true;
                }
                else
                    return new BotReturnStatus(false, string.Format("You are not registered. You can only reset your password if you are a registered user.", username));
            }
            else
            {
                if (Security.RoleManager.IsInAncestorTree(source.SecurityIdentity.Role, user.SecurityIdentity.Role))
                {
                    if (user.SecurityIdentity.IsRegistered)
                    {
                        this.ParentBot.SendPrivateMessageFromBot(user, "Please enter a new password using the !pass command. You must remember this password to connect to the hub.\nUsage: !pass \"newpassword\"");
                        user.SecurityIdentity.NewPasswordRequestActive = true;
                    }
                    else
                        return new BotReturnStatus(false, string.Format("The user '{0}' is not registered. Can only reset the password of a registered user.", username));
                }
                else
                    return new BotReturnStatus(false, string.Format("Permission denied. You are not an ancestor of '{0}'.", username));
            }

            return new BotReturnStatus(true);
        }
    }
}
